#pragma once
#include "packet.h"
#include "LockEventEx.h"

#define TCPC_TEMPSIZE     1024*16
#define TCPC_SENDSIZE     1024*16
#define TCPC_RECVSIZE     1024*64
#define TCPC_PACKHEAD     (DWORD)0xAAAAAAAA

class CTcpClient
{
private:
    virtual void OnClose() = 0;
    virtual void OnConnect() = 0;
    virtual void OnRecvData( char* pBuff, int nSize ) = 0;

    BOOL m_bUsedPack;
    BOOL m_bConnectOk;

    HANDLE m_hThread;
    SOCKET m_sockClient;
    CLockSec  m_lockSection;

    FD_SET ReadSet;
    FD_SET WriteSet;
    DWORD  m_dwSendSize, m_dwRecvSize;
    char   m_uzRecvBuff[TCPC_RECVSIZE];
    char   m_uzSendBuff[TCPC_SENDSIZE];

    BOOL RecvData();
    static DWORD WINAPI RecvWorkThread( LPVOID lpParameter );

public:
    CTcpClient(void);
    virtual ~CTcpClient(void);

    void StopWork();
    void SendDataToServer( char* pBuff, int nSize );
    BOOL TestServer( LPCSTR lpAddr, USHORT uPort );
    BOOL BeginWork( ULONG dwAddr, USHORT uPort, BOOL bUsePack );
    BOOL BeginWork( LPCSTR lpAddr, USHORT uPort, BOOL bUsePack );
};
